<?php
/**
 * (logic) （临时转正式）VAT费
 */
namespace App\Logic\MonthlyReport\ParseTmp;

use App\Models\Basics\SysCountry;
use App\Models\MonthlyReport\Payment\MRVat;
use App\Models\MonthlyReport\Tmp\TmpVAT;
class ParseVat
{
    public static function exeParse($date){
        //临时
        $lines = TmpVAT::query()->selectRaw('country,sum(amount) as amount')
            ->where('report_date',$date)
            ->groupBy('country')
            ->get();
        $i = 0;
        $num = 100;
        $country = [];
        while(true){
            if($lines->isEmpty())break;
            $_lines = $lines->splice($i,$num);
            foreach ($_lines as $line){
                if(!array_key_exists($line->country,$country)){
                    if($line->country == 'EU'){
                        $countryId = SysCountry::EU_COUNTRY_ID;
                    }else{
                        $_countryId = SysCountry::getIdByCountry($line->country);
                        $countryId = $_countryId ?: 0;
                    }
                    $country[$line->country] = $countryId;
                }
                //正式
                MRVat::query()->updateOrCreate(['report_date' => $date,
                    'country_id' => $country[$line->country]],
                    ['amount' => $line->amount]);
            }
        }
    }
}
